<?php
class Data_Model_LopHocMapper extends Data_Model_DataMapperAbstract
{
	private static $_instance = null;
	// dung de phan trang
	private $_paginator = null;
	
	
	public function searchByMalophoc($malophoc)
	{
		$db = $this->getDb();
		$selectLophoc = $db->select()
		->from('tbl_lophoc')
		->where('MaLop = ?',$malophoc);
		$lophoc = $db->fetchRow($selectLophoc);
		
		$loaidaotaoMapper = Data_Model_LoaiDaoTaoMapper::getInstance();
		$loaidaotao = $loaidaotaoMapper->searchById($lophoc['MaLoaiDaoTao']);
		$lophoc['tbl_loaidaotao'] = $loaidaotao;
		
		$chitietMapper = Data_Model_ChiTietLopHocMapper::getInstance();
		$chitiet = $chitietMapper->searchByMalophoc($lophoc['MaLop']);
		$lophoc['tbl_chitietlophoc'] = $chitiet;
		
		return $this->_populate($lophoc);
	}
	public function searchById($malophoc)
	{
		$db = $this->getDb();
		$selectLophoc = $db->select()
						   ->from('tbl_lophoc')
						   ->where('MaLop = ?',$malophoc);
		$lophoc = $db->fetchRow($selectLophoc);
		
		$loaidaotaoMapper = Data_Model_LoaiDaoTaoMapper::getInstance();
		$loaidaotao = $loaidaotaoMapper->searchById($lophoc['MaLoaiDaoTao']);
		$lophoc['tbl_loaidaotao'] = $loaidaotao;
		
		/* $dsthiMapper = Data_Model_DanhSachThiMapper::getInstance();
		$dsthi = $dsthiMapper->searchByMalophoc($malophoc);
		$lophoc['tbl_danhsachthi'] = $dsthi; */
		
		$chitietMapper = Data_Model_ChiTietLopHocMapper::getInstance();
		$chitiet = $chitietMapper->searchByMalophoc($lophoc['MaLop']);
		$lophoc['tbl_chitietlophoc'] = $chitiet;
		
		return $this->_populate($lophoc);
	}
	
	public function lophocList()
	{
		$db = $this->getDb();
		$selectLophoc = $db->select()
						   ->from('tbl_lophoc');
		$lophocs = $db->fetchAll($selectLophoc);
		$lophocObjArray = array();
		foreach($lophocs as $lophoc)
		{
			/* $diemthiMapper = Data_Model_DiemThiMapper::getInstance();
			$diemthi = $diemthiMapper->searchByMalophoc($lophoc['MaLop']);
			$lophoc['tbl_diemthi'] = $diemthi; */
			
			$loaidaotaoMapper = Data_Model_LoaiDaoTaoMapper::getInstance();
			$loaidaotao = $loaidaotaoMapper->searchById($lophoc['MaLoaiDaoTao']);
			$lophoc['tbl_loaidaotao'] = $loaidaotao;
			
			/* $dsthiMapper = Data_Model_DanhSachThiMapper::getInstance();
			$dsthi = $dsthiMapper->searchByMalophoc($lophoc['MaLop']);
			$lophoc['tbl_danhsachthi'] = $dsthi; */
			
			$chitietMapper = Data_Model_ChiTietLopHocMapper::getInstance();
			$chitiet = $chitietMapper->searchByMalophoc($lophoc['MaLop']);
			$lophoc['tbl_chitietlophoc'] = $chitiet;
			
			$lophocObjArray[] = $this->_populate($lophoc);
		}
		return $lophocObjArray;
	}
	// hien thi
	protected function _populate($data)
	{
		$obj = new Data_Model_LopHoc();
		$obj->setId($data['MaLop']);
		$obj->setMaloaidt($data['MaLoaiDaoTao']);
		$obj->setTenlop($data['TenLop']);
		$obj->setSotiet($data['SoTiet']);
		$obj->setThoigian($data['ThoiGian']);
		$obj->setHocphi($data['HocPhi']);
		$obj->setTrangthai($data['TrangThai']);
		$obj->setChitietlophoc($data['tbl_chitietlophoc']);
		$obj->setLoaidaotao($data['tbl_loaidaotao']);/* 
		$obj->setDiemthi($data['tbl_diemthi']);
		$obj->setKythi($data['tbl_kythi']); 
		$obj->setDanhsachthi($data['tbl_danhsachthi']); */
		return $obj;
	}
	protected function _insert(Data_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data = array(
				'MaLoaiDaoTao' => $obj->getMaloaidt(),
				'TenLop' => $obj->getTenlop(),
				'SoTiet' => $obj->getSotiet(),
				'ThoiGian' => $obj->getThoigian(),
				'HocPhi' => $obj->getHocphi(),
				'TrangThai' => $obj->getTrangthai()
				);
		$db->insert('tbl_lophoc',$data);
	}
	protected function _update(Data_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data = array(
				'MaLoaiDaoTao' => $obj->getMaloaidt(),
				'TenLop' => $obj->getTenlop(),
				'SoTiet' => $obj->getSotiet(),
				'ThoiGian' => $obj->getThoigian(),
				'HocPhi' => $obj->getHocphi(),
				'TrangThai' => $obj->getTrangthai()
		);
		$where = $db->quoteInto('MaLop = ?',$obj->getId());
		$db->update('tbl_lophoc',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaLop = ?',$id);
		$db->delete('tbl_lophoc',$where);
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}